package com.nullpointer.xchatfinal.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.ViewImageActivity;
import com.nullpointer.xchatfinal.object.ChatType;
import com.nullpointer.xchatfinal.object.DetailedMessage;
import com.nullpointer.xchatfinal.object.HistoryMessage;
import com.nullpointer.xchatfinal.object.MessageContent;
import com.nullpointer.xchatfinal.object.MessageContent.MsgType;
import com.nullpointer.xchatfinal.utils.Common;
import com.nullpointer.xchatfinal.utils.Emoticons;
import com.nullpointer.xchatfinal.utils.Utils;

public class HistoryMessageAdapter extends ArrayAdapter<HistoryMessage> {

	private static class ViewHolder {
		public final LinearLayout rootView;
		public final ImageView ivAvatar;
		public final TextView tvName;
		public final TextView tvMessage;
		public final TextView tvMessageNum;

		private ViewHolder(LinearLayout rootView, ImageView ivAvatar,
				TextView tvName, TextView tvMessage, TextView tvMessageNum) {
			this.rootView = rootView;
			this.ivAvatar = ivAvatar;
			this.tvName = tvName;
			this.tvMessage = tvMessage;
			this.tvMessageNum = tvMessageNum;
		}

		public static ViewHolder create(LinearLayout rootView) {
			ImageView ivAvatar = (ImageView) rootView
					.findViewById(R.id.iv_avatar);
			TextView tvName = (TextView) rootView.findViewById(R.id.tv_name);
			TextView tvMessage = (TextView) rootView
					.findViewById(R.id.tv_message);
			TextView tvMessageNum = (TextView) rootView
					.findViewById(R.id.tv_message_num);
			return new ViewHolder(rootView, ivAvatar, tvName, tvMessage,
					tvMessageNum);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_history, parent,
					false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		HistoryMessage item = getItem(position);
		DetailedMessage detailedMessage = item.getDetailedMessage();

		int messageNum = item.getUnreadNum();

		if (messageNum == 0) {
			vh.tvMessageNum.setText("");
			vh.tvMessageNum.setVisibility(View.GONE);
		} else {
			if (messageNum > 9) {
				vh.tvMessageNum.setText("9+");
			} else {
				vh.tvMessageNum.setText(String.valueOf(messageNum));
			}
			vh.tvMessageNum.setVisibility(View.VISIBLE);
		}

		MessageContent messageContent = detailedMessage.getMessageContent();
		vh.tvName.setText(detailedMessage.getTargetName());

		if (item.getChatType() == ChatType.CHAT_GROUP) {
			vh.ivAvatar.setImageResource(R.drawable.ic_group_avatar);
			if (messageContent.getMsgType() == MsgType.TEXT
					|| messageContent.getMsgType() == MsgType.UPDATE_GROUP_INFO) {
				vh.tvMessage.setText(messageContent.getName()
						+ ": "
						+ Emoticons.getSmiledText(context,
								messageContent.getContent()));
			} else {
				vh.tvMessage.setText(messageContent.getName() + ": "
						+ "Send an image!");
			}
		} else {
			final String avatarID = messageContent.getAvatarID();
			if (avatarID != null) {
				ImageOptions options = new ImageOptions();
				options.round = 5;
				options.fileCache = true;
				options.memCache = true;
				options.targetWidth = 48;
				options.fallback = R.drawable.ic_private_avatar;
				String url = Utils.getSmallAvatarFacebook(avatarID);
				AQuery aq = new AQuery(convertView);
				aq.id(vh.ivAvatar).image(url, options);
				vh.ivAvatar.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						String avatarLarge = Utils
								.getLargeAvatarFacebook(avatarID);
						Intent intent = new Intent(context,
								ViewImageActivity.class);
						intent.putExtra(Common.ExtraKey.IMAGE_URL, avatarLarge);
						context.startActivity(intent);
					}
				});
			} else {
				vh.ivAvatar.setImageResource(R.drawable.ic_private_avatar);
			}

			if (messageContent.getMsgType() == MsgType.TEXT) {
				vh.tvMessage.setText(Emoticons.getSmiledText(context,
						messageContent.getContent()));
			} else if (messageContent.getMsgType() == MsgType.SEND_CHAT_VIDEO_REQUEST) {
				vh.tvMessage.setText("You sent a video calling request...");
			} else if (messageContent.getMsgType() == MsgType.RECEIVE_CHAT_VIDEO_REQUEST) {
				vh.tvMessage.setText(messageContent.getName()
						+ " sent a video calling request...");
			} else if (messageContent.getMsgType() == MsgType.ACCEPT_VIDEO_CALL) {
				vh.tvMessage.setText(messageContent.getContent());
			} else if (messageContent.getMsgType() == MsgType.REJECT_VIDEO_CALL) {
				vh.tvMessage.setText(messageContent.getContent());
			} else if (messageContent.getMsgType() == MsgType.IMAGE) {
				vh.tvMessage.setText("Send an image!");
			}
		}
		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private Context context;

	// Constructors
	public HistoryMessageAdapter(Context context, List<HistoryMessage> objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public HistoryMessageAdapter(Context context, HistoryMessage[] objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}
}
